using System.Collections;
using System.Collections.Generic;
using System.Linq;
using QFramework;
using UnityEngine;

namespace ProjectSurvivor
{
    public class ExpUpgradeSystem : AbstractSystem
    {
        public List<ExpUpgradeItem> Items = new List<ExpUpgradeItem>();

        public Dictionary<string, string> Pairs = new Dictionary<string, string>()
        {
            { "simple_sword", "critical_rate" },
            { "simple_knife", "damage_rate" },
            { "rotate_sword", "addtion_flying_count" },
            { "simple_ball", "movement_rate" },

            { "critical_rate", "simple_sword" },
            { "damage_rate", "simple_knife" },
            { "addtion_flying_count", "rotate_sword" },
            { "movement_rate", "simple_ball" },
        };

        public Dictionary<string, ExpUpgradeItem> DictionaryExpUpgradeItems = new Dictionary<string, ExpUpgradeItem>();
        
        public Dictionary<string, BindableProperty<bool>> PairsProperties = new Dictionary<string, BindableProperty<bool>>()
        {
            { "simple_sword", Global.SuperSimpleSword },
            { "simple_knife", Global.SuperSimpleKnife },
            { "rotate_sword", Global.SuperSimpleRotateSword },
            { "simple_ball", Global.SuperSimpleBall }
        };
        
        public ExpUpgradeItem Add(ExpUpgradeItem item)
        {
            Items.Add(item);
            return item;
        }
        protected override void OnInit()
        {
            ResetData();
            Global.Level.Register(level =>
            {
                Roll();
            });
        }

        public void ResetData()
        {
            Items.Clear();
            
            // 剑
            Add(new ExpUpgradeItem(true)
                .WithKey("simple_sword")
                .WithName("剑")
                .WithIconName("simple_sword_icon")
                .WithPairedName("合成后的剑")
                .WithPairedIconName("paired_simple_sword_icon")
                .WithPairedDescription("攻击力翻倍 攻击范围翻倍")
                .WithDescription(lv =>
                {
                    return lv switch
                    {
                        1 => $"剑lv{lv}:攻击身边的敌人",
                        2 => $"剑lv{lv}:\n攻击力+3 数量+2",
                        3 => $"剑lv{lv}:\n攻击力+2 间隔-0.25s",
                        4 => $"剑lv{lv}:\n攻击力+2 间隔-0.25s",
                        5 => $"剑lv{lv}:\n攻击力+3 数量+2",
                        6 => $"剑lv{lv}:\n范围+1 间隔-0.25s",
                        7 => $"剑lv{lv}:\n攻击力+3 数量+2",
                        8 => $"剑lv{lv}:\n攻击力+2 范围+2",
                        9 => $"剑lv{lv}:\n攻击力+3 间隔-0.25s",
                        10 => $"剑lv{lv}:\n攻击力+3 数量+2",
                        _ => null
                    };
                }))
                .WithMaxLevel(10)
                .OnUpgrade((_, level) =>
                {
                    switch (level)
                    {
                        case 1:
                            Global.SimpleSwordUnlock.Value = true;
                            break;
                        case 2:
                            Global.SimpleSwordDamage.Value += 3;
                            Global.SimpleSwordCount.Value += 2;
                            break;
                        case 3:
                            Global.SimpleSwordDamage.Value += 2;
                            Global.SimpleSwordDuration.Value -= 0.25f;
                            break;
                        case 4:
                            Global.SimpleSwordDamage.Value += 3;
                            Global.SimpleSwordDuration.Value -= 0.25f;
                            break;
                        case 5:
                            Global.SimpleSwordDamage.Value += 3;
                            Global.SimpleSwordCount.Value += 2;
                            break;
                        case 6:
                            Global.SimpleSwordRange.Value += 1;
                            Global.SimpleSwordDuration.Value -= 0.25f;
                            break;
                        case 7:
                            Global.SimpleSwordDamage.Value += 3;
                            Global.SimpleSwordCount.Value += 2;
                            break;
                        case 8:
                            Global.SimpleSwordDamage.Value += 2;
                            Global.SimpleSwordRange.Value += 2;
                            break;
                        case 9:
                            Global.SimpleSwordDamage.Value += 3;
                            Global.SimpleSwordDuration.Value -= 0.25f;
                            break;
                        case 10:
                            Global.SimpleSwordDamage.Value += 3;
                            Global.SimpleSwordCount.Value += 2;
                            break;
                    }
                });
            
            // 刀
            Add(new ExpUpgradeItem(true)
                .WithKey("simple_knife")
                .WithName("飞刀")
                .WithIconName("simple_knife_icon")
                .WithPairedName("合成后的飞刀")
                .WithPairedIconName("paired_simple_knife_icon")
                .WithPairedDescription("攻击力翻倍")
                .WithDescription(lv =>
                {
                    return lv switch
                    {
                        1 => $"飞刀lv{lv}:\n向最近的敌人发射一把飞刀",
                        2 => $"飞刀lv{lv}:\n攻击力+3 数量+2",
                        3 => $"飞刀lv{lv}:\n攻击力+2 间隔-0.25s",
                        4 => $"飞刀lv{lv}:\n攻击力+2 间隔-0.25s",
                        5 => $"飞刀lv{lv}:\n攻击力+3 数量+2 穿透+1",
                        6 => $"飞刀lv{lv}:\n攻击力+1 间隔-0.25s",
                        7 => $"飞刀lv{lv}:\n攻击力+3 数量+2",
                        8 => $"飞刀lv{lv}:\n攻击力+2 数量+2 穿透+1",
                        9 => $"飞刀lv{lv}:\n攻击力+3 间隔-0.25s",
                        10 => $"飞刀lv{lv}:\n攻击力+3 数量+2",
                        _ => null
                    };
                }))
                .WithMaxLevel(10)
                .OnUpgrade((_, level) =>
                {
                    switch (level)
                    {
                        case 1:
                            Global.SimpleKnifeUnlock.Value = true;
                            break;
                        case 2:
                            Global.SimpleKnifeDamage.Value += 3;
                            Global.SimpleKnifeCount.Value += 2;
                            break;
                        case 3:
                            Global.SimpleKnifeDamage.Value += 2;
                            Global.SimpleKnifeDuration.Value -= 0.25f;
                            break;
                        case 4:
                            Global.SimpleKnifeDamage.Value += 3;
                            Global.SimpleKnifeDuration.Value -= 0.25f;
                            break;
                        case 5:
                            Global.SimpleKnifeDamage.Value += 3;
                            Global.SimpleKnifeCount.Value += 2;
                            Global.SimpleKnifeAttackCount.Value++;
                            break;
                        case 6:
                            Global.SimpleKnifeDamage.Value += 1;
                            Global.SimpleKnifeDuration.Value -= 0.25f;
                            break;
                        case 7:
                            Global.SimpleKnifeDamage.Value += 3;
                            Global.SimpleKnifeCount.Value += 2;
                            break;
                        case 8:
                            Global.SimpleKnifeDamage.Value += 2;
                            Global.SimpleKnifeCount.Value += 2;
                            Global.SimpleKnifeAttackCount.Value++;
                            break;
                        case 9:
                            Global.SimpleKnifeDamage.Value += 3;
                            Global.SimpleKnifeDuration.Value -= 0.25f;
                            break;
                        case 10:
                            Global.SimpleKnifeDamage.Value += 3;
                            Global.SimpleKnifeCount.Value += 2;
                            break;
                    }
                });
            
            //  守卫剑
            Add(new ExpUpgradeItem(true)
                .WithKey("rotate_sword")
                .WithName("守卫剑")
                .WithIconName("rotate_sword_icon")
                .WithPairedName("合成后的守卫剑")
                .WithPairedIconName("paired_rotate_sword_icon")
                .WithPairedDescription("攻击力翻倍 旋转速度翻倍")
                .WithDescription(lv =>
                {
                    return lv switch
                    {
                        1 => $"守卫剑lv{lv}:\n围绕自身旋转的剑",
                        2 => $"守卫剑lv{lv}:\n攻击力+3 数量+1",
                        3 => $"守卫剑lv{lv}:\n攻击力+2 速度+1",
                        4 => $"守卫剑lv{lv}:\n攻击力+2 速度+1",
                        5 => $"守卫剑lv{lv}:\n攻击力+3 数量+1 范围+1",
                        6 => $"守卫剑lv{lv}:\n攻击力+1 速度+1",
                        7 => $"守卫剑lv{lv}:\n攻击力+3 数量+1",
                        8 => $"守卫剑lv{lv}:\n攻击力+2 数量+1 范围+1",
                        9 => $"守卫剑lv{lv}:\n攻击力+3 速度+1",
                        10 => $"守卫剑lv{lv}:\n攻击力+3 数量+1",
                        _ => null
                    };
                }))
                .WithMaxLevel(10)
                .OnUpgrade((_, level) =>
                {
                    switch (level)
                    {
                        case 1:
                            Global.SimpleRotateSwordUnlock.Value = true;
                            break;
                        case 2:
                            Global.SimpleRotateSwordDamage.Value += 3;
                            Global.SimpleRotateSwordCount.Value += 1;
                            break;
                        case 3:
                            Global.SimpleRotateSwordDamage.Value += 2;
                            Global.SimpleRotateSwordSpeed.Value += 1;
                            break;
                        case 4:
                            Global.SimpleRotateSwordDamage.Value += 3;
                            Global.SimpleRotateSwordSpeed.Value += 1;
                            break;
                        case 5:
                            Global.SimpleRotateSwordDamage.Value += 3;
                            Global.SimpleRotateSwordCount.Value += 1;
                            Global.SimpleRotateSwordRange.Value++;
                            break;
                        case 6:
                            Global.SimpleRotateSwordDamage.Value += 1;
                            Global.SimpleRotateSwordSpeed.Value += 1;
                            break;
                        case 7:
                            Global.SimpleRotateSwordDamage.Value += 3;
                            Global.SimpleRotateSwordCount.Value += 1;
                            break;
                        case 8:
                            Global.SimpleRotateSwordDamage.Value += 2;
                            Global.SimpleRotateSwordCount.Value += 1;
                            Global.SimpleRotateSwordRange.Value++;
                            break;
                        case 9:
                            Global.SimpleRotateSwordDamage.Value += 3;
                            Global.SimpleRotateSwordSpeed.Value += 1;
                            break;
                        case 10:
                            Global.SimpleRotateSwordDamage.Value += 3;
                            Global.SimpleRotateSwordCount.Value += 1;
                            break;
                    }
                });
            
             // 篮球
            Add(new ExpUpgradeItem(true)
                .WithKey("simple_ball")
                .WithName("篮球")
                .WithIconName("ball_icon")
                .WithPairedName("合成后的篮球")
                .WithPairedIconName("paired_ball_icon")
                .WithPairedDescription("攻击力翻倍 体积变大")
                .WithDescription(lv =>
                {
                    return lv switch
                    {
                        1 => $"篮球lv{lv}:会一直弹来弹去",
                        2 => $"篮球lv{lv}:\n攻击力+3 数量+1",
                        3 => $"篮球lv{lv}:\n攻击力+2 速度+1",
                        4 => $"篮球lv{lv}:\n攻击力+2 速度+1",
                        5 => $"篮球lv{lv}:\n攻击力+3 数量+1",
                        6 => $"篮球lv{lv}:\n攻击力+1 速度+1",
                        7 => $"篮球lv{lv}:\n攻击力+3 数量+1",
                        8 => $"篮球lv{lv}:\n攻击力+2 数量+1",
                        9 => $"篮球lv{lv}:\n攻击力+3 速度+1",
                        10 => $"篮球lv{lv}:\n攻击力+3 数量+1",
                        _ => null
                    };
                }))
                .WithMaxLevel(10)
                .OnUpgrade((_, level) =>
                {
                    switch (level)
                    {
                        case 1:
                            Global.SimpleBallUnlock.Value = true;
                            break;
                        case 2:
                            Global.SimpleBallDamage.Value += 3;
                            Global.SimpleBallCount.Value += 1;
                            break;
                        case 3:
                            Global.SimpleBallDamage.Value += 2;
                            Global.SimpleBallSpeed.Value += 1;
                            break;
                        case 4:
                            Global.SimpleBallDamage.Value += 3;
                            Global.SimpleBallSpeed.Value += 1;
                            break;
                        case 5:
                            Global.SimpleBallDamage.Value += 3;
                            Global.SimpleBallCount.Value += 1;
                            break;
                        case 6:
                            Global.SimpleBallDamage.Value += 1;
                            Global.SimpleBallSpeed.Value += 1;
                            break;
                        case 7:
                            Global.SimpleBallDamage.Value += 3;
                            Global.SimpleBallCount.Value += 1;
                            break;
                        case 8:
                            Global.SimpleBallDamage.Value += 2;
                            Global.SimpleBallCount.Value += 1;
                            break;
                        case 9:
                            Global.SimpleBallDamage.Value += 3;
                            Global.SimpleBallSpeed.Value += 1;
                            break;
                        case 10:
                            Global.SimpleBallDamage.Value += 3;
                            Global.SimpleBallCount.Value += 1;
                            break;
                    }
                });
            
            // 炸弹
            Add(new ExpUpgradeItem(false)
                .WithKey("bomb")
                .WithName("炸弹")
                .WithIconName("bomb_icon")
                .WithPairedName("合成后的炸弹")
                .WithPairedIconName("paired_bomb_icon")
                .WithPairedDescription("爆炸")
                .WithDescription(lv =>
                {
                    return lv switch
                    {
                        1 => $"炸弹lv{lv}:对全屏敌人造成伤害",
                        2 => $"炸弹lv{lv}:\n攻击力+1 掉落概率+0.05",
                        3 => $"炸弹lv{lv}:\n攻击力+1 掉落概率+0.05",
                        4 => $"炸弹lv{lv}:\n攻击力+1 掉落概率+0.05",
                        5 => $"炸弹lv{lv}:\n攻击力+1 掉落概率+0.05",
                        _ => null
                    };
                }))
                .WithMaxLevel(5)
                .OnUpgrade((_, level) =>
                {
                    switch (level)
                    {
                        case 1:
                            Global.BombUnlock.Value = true;
                            break;
                        case 2:
                            Global.BombDamage.Value += 1;
                            Global.BombPercent.Value += 0.05f;
                            break;
                        case 3:
                            Global.BombDamage.Value += 1;
                            Global.BombPercent.Value += 0.05f;
                            break;
                        case 4:
                            Global.BombDamage.Value += 1;
                            Global.BombPercent.Value += 0.05f;
                            break;
                        case 5:
                            Global.BombDamage.Value += 1;
                            Global.BombPercent.Value += 0.05f;
                            break;
                    }
                });
            
            // 暴击
            Add(new ExpUpgradeItem(false)
                .WithKey("critical_rate")
                .WithName("暴击")
                .WithIconName("critical_icon")
                .WithDescription(lv =>
                {
                    return lv switch
                    {
                        1 => $"暴击lv{lv}:暴击概率+0.05",
                        2 => $"暴击lv{lv}:暴击概率+0.05",
                        3 => $"暴击lv{lv}:暴击概率+0.05",
                        4 => $"暴击lv{lv}:暴击概率+0.05",
                        5 => $"暴击lv{lv}:暴击概率+0.05",
                        _ => null
                    };
                }))
                .WithMaxLevel(5)
                .OnUpgrade((_, level) =>
                {
                    switch (level)
                    {
                        case 1:
                            Global.CriticalRate.Value += 0.05f;
                            break;
                        case 2:
                            Global.CriticalRate.Value += 0.05f;
                            break;
                        case 3:
                            Global.CriticalRate.Value += 0.05f;
                            break;
                        case 4:
                            Global.CriticalRate.Value += 0.05f;
                            break;
                        case 5:
                            Global.CriticalRate.Value += 0.05f;
                            break;
                    }
                });
            
            // 额外伤害
            Add(new ExpUpgradeItem(false)
                    .WithKey("damage_rate")
                    .WithName("伤害率")
                    .WithIconName("damage_icon")
                    .WithDescription(lv =>
                    {
                        return lv switch
                        {
                            1 => $"额外伤害lv{lv}:伤害+1",
                            2 => $"额外伤害lv{lv}:伤害+1.2",
                            3 => $"额外伤害lv{lv}:伤害+1.4",
                            4 => $"额外伤害lv{lv}:伤害+1.6",
                            5 => $"额外伤害lv{lv}:伤害+2",
                            _ => null
                        };
                    }))
                .WithMaxLevel(5)
                .OnUpgrade((_, level) =>
                {
                    switch (level)
                    {
                        case 1:
                            Global.DamageRate.Value += 1f;
                            break;
                        case 2:
                            Global.DamageRate.Value += 1.2f;
                            break;
                        case 3:
                            Global.DamageRate.Value += 1.4f;
                            break;
                        case 4:
                            Global.DamageRate.Value += 1.6f;
                            break;
                        case 5:
                            Global.DamageRate.Value += 2f;
                            break;
                    }
                });
            
            // 额外飞行物
            Add(new ExpUpgradeItem(false)
                    .WithKey("addtion_flying_count")
                    .WithName("飞射物")
                    .WithIconName("fly_icon")
                    .WithDescription(lv =>
                    {
                        return lv switch
                        {
                            1 => $"额外飞行物lv{lv}:数量+1",
                            2 => $"额外飞行物lv{lv}:数量+1",
                            3 => $"额外飞行物lv{lv}:数量+1",
                            _ => null
                        };
                    }))
                .WithMaxLevel(3)
                .OnUpgrade((_, level) =>
                {
                    switch (level)
                    {
                        case 1:
                            Global.AdditionFlyingCount.Value += 1;
                            break;
                        case 2:
                            Global.AdditionFlyingCount.Value += 1;
                            break;
                        case 3:
                            Global.AdditionFlyingCount.Value += 1;
                            break;
                    }
                });
            
            // 额外移动速度
            Add(new ExpUpgradeItem(false)
                    .WithKey("movement_rate")
                    .WithName("移动速度")
                    .WithIconName("movement_icon")
                    .WithDescription(lv =>
                    {
                        return lv switch
                        {
                            1 => $"额外移动速度lv{lv}:移速增加20%",
                            2 => $"额外移动速度lv{lv}:移速增加30%",
                            3 => $"额外移动速度lv{lv}:移速增加20%",
                            4 => $"额外移动速度lv{lv}:移速增加30%",
                            5 => $"额外移动速度lv{lv}:移速增加20%",
                            _ => null
                        };
                    }))
                .WithMaxLevel(5)
                .OnUpgrade((_, level) =>
                {
                    switch (level)
                    {
                        case 1:
                            Global.MovementSpeedRate.Value += 0.2f;
                            break;
                        case 2:
                            Global.MovementSpeedRate.Value += 0.3f;
                            break;
                        case 3:
                            Global.MovementSpeedRate.Value += 0.2f;
                            break;
                        case 4:
                            Global.MovementSpeedRate.Value += 0.3f;
                            break;
                        case 5:
                            Global.MovementSpeedRate.Value += 0.2f;
                            break;
                    }
                });
            
            // 拾取范围
            Add(new ExpUpgradeItem(false)
                    .WithKey("collectable_area")
                    .WithName("拾取范围")
                    .WithIconName("collectable_icon")
                    .WithDescription(lv =>
                    {
                        return lv switch
                        {
                            1 => $"拾取范围lv{lv}:增加150%",
                            2 => $"拾取范围lv{lv}:增加250%",
                            3 => $"拾取范围lv{lv}:增加150%",
                            _ => null
                        };
                    }))
                .WithMaxLevel(3)
                .OnUpgrade((_, level) =>
                {
                    switch (level)
                    {
                        case 1:
                            Global.CollectableArea.Value += 1.5f;
                            break;
                        case 2:
                            Global.CollectableArea.Value += 2.5f;
                            break;
                        case 3:
                            Global.CollectableArea.Value += 1.5f;
                            break;
                    }
                });
            
            DictionaryExpUpgradeItems = Items.ToDictionary(item => item.Key);
        }
        public void Roll()
        {
            foreach (var expUpgradeItem in Items)
            {
                expUpgradeItem.Visible.Value = false;
            }
            
            var items = Items.Where(item => !item.UpgradeFinish).ToList();

            if (items.Count >= 4)
            {
                items.GetAndRemoveRandomItem().Visible.Value = true;
                items.GetAndRemoveRandomItem().Visible.Value = true;
                items.GetAndRemoveRandomItem().Visible.Value = true;
                items.GetAndRemoveRandomItem().Visible.Value = true;
            }
            else
            {
                foreach (var item in items)
                {
                    item.Visible.Value = true;
                }
            }
        }
        
    }
}

